package util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JdbcUtils {
    static {
        try{
            Class.forName("com.mysql.jdbc.Driver");
        }catch (ClassNotFoundException e){
            e.printStackTrace();
        }
    }


    //获取Connection对象
    public static Connection getConnection() throws SQLException {
        Connection conn = null;
        String url = "jdbc:mysql://localhost:3306/web_jsp";
        String username = "root";
        String password = "root";


        conn =  DriverManager.getConnection(url,username,password);
        return conn;

    }

    //关闭资源
    public static void close(Connection conn, Statement stmt, ResultSet rs) throws SQLException {

        if (conn != null && !conn.isClosed() ){
            conn.close();
        }
        if (stmt != null && !stmt.isClosed() ){
            stmt.close();
        }
        if (rs != null && !rs.isClosed() ){
            rs.close();
        }

    }
}
